package beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import model.Proprietate;
import conversions.Conversions;

/**
 * Session Bean implementation class ProprietateDAO
 */
@Stateless
public class ProprietateDAO implements ProprietateDAORemote {

	@PersistenceContext
	EntityManager em;
	
    /**
     * Default constructor. 
     */
    public ProprietateDAO() {
        // TODO Auto-generated constructor stub
    }
    
    public void insertProperty(ProprietateDTO prop) {
		Proprietate pr = new Conversions().fromDTO(prop);

		// pr.setAdresa(prop.getAdresa());
		em.persist(pr);
	}
    
    @Override
	public List<ProprietateDTO> getProprietateList() {

		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Proprietate> cq = cb.createQuery(Proprietate.class);
		Root<Proprietate> prop = cq.from(Proprietate.class);

		// cq.select(casa).where(cb.ge(casa.get(Casa_.pret),pret));

		cq.select(prop);

		TypedQuery<Proprietate> q = em.createQuery(cq);
		List<Proprietate> props = q.getResultList();

		List<ProprietateDTO> propsDTO = new ArrayList<>();
		for (Proprietate p : props) {
			propsDTO.add(new Conversions().toDTO(p));
		}
		return propsDTO;
	}

}
